//
//  MHRecentMessageTableViewCell.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/5/30.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHRecentMessageTableViewCell.h"
#import "MHRecentMessageItemViewModel.h"
#import "NSMutableAttributedString+MHMoment.h"
#import "MHPicture.h"

@interface MHRecentMessageTableViewCell()
//@property(nonatomic,strong) MHAskItemViewModel *viewModel;
@property(nonatomic, strong) YYLabel *contentyyLabel;
@property(nonatomic, strong) YYTextLayout *nameLableLayout;

@end

@implementation MHRecentMessageTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
//    self.nameLable.font =
    self.contentLable.font = MHMomentContentFont;
    self.createTimeLabel.font = MHRegularFont_12;
    self.nameLable.font = MHRegularFont_14;
    self.cirleName.font = MHRegularFont_14;
    self.attrImage.clipsToBounds = YES;
    self.contentLable.hidden = YES;
    [self addSubview:self.contentyyLabel];
    
}


-(void)bindViewModel:(MHRecentMessageItemViewModel *)viewModel
{
//    RAC(self.nameLable,text) = RACObserve(viewModel.recentMessage, oprtUserName);
    self.nameLable.text = viewModel.recentMessage.oprtUserName;
    self.createTimeLabel.text = viewModel.recentMessage.transformTime;
    [self.avterImageView yy_setImageWithURL:[NSURL URLWithString:viewModel.recentMessage.fullOprtUserFace] placeholder:MHDefaultAvatar(MHDefaultAvatarTypeDefualt)];
    self.attrImage.hidden = (viewModel.recentMessage.attmList.count <= 0 && !viewModel.recentMessage.vedioObj);
    self.cirleName.text = [NSString stringWithFormat:@"(%@)",viewModel.recentMessage.circleName];
    YYTextLayout  *nameLableLayout;
    if(viewModel.recentMessage.attmList.count >0)
    {
        MHPicture *pic =  [viewModel.recentMessage.attmList lastObject];
        [self.attrImage yy_setImageWithURL:[NSURL URLWithString:pic.fullPath] placeholder:MHDefaultAvatar(MHDefaultAvatarTypeDefualt)];
    }else if (viewModel.recentMessage.vedioObj)
    {
         [self.attrImage yy_setImageWithURL:viewModel.recentMessage.vedioObj.coverURL placeholder:MHDefaultAvatar(MHDefaultAvatarTypeDefualt)];
    }
    switch (viewModel.recentMessage.type) {
        case MHRecentMessageTypeComment:
        {
            NSMutableAttributedString *textAttr = [[NSMutableAttributedString alloc] initWithString:viewModel.recentMessage.contentTxt];
            textAttr.yy_font = MHMomentContentFont;
            textAttr.yy_color = MHMomentContentTextColor;
            textAttr.yy_lineBreakMode = NSLineBreakByCharWrapping;
            textAttr.yy_alignment = NSTextAlignmentLeft;
            [textAttr mh_regexContentWithWithEmojiImageFontSize:16];
            self.contentLable.attributedText = textAttr;
//            self.contentyyLabel.attributedText  = textAttr;
            
            YYTextContainer *nameLableContainer = [YYTextContainer containerWithSize:CGSizeMake(MH_SCREEN_WIDTH - 55*2- 30-23, MAXFLOAT)];
            nameLableContainer.maximumNumberOfRows = 0;
            nameLableLayout = [YYTextLayout layoutWithContainer:nameLableContainer text:textAttr.copy];
        self.contentyyLabel.textLayout = nameLableLayout;
//            self.contentyyLabel.frame = CGRectMake(self.contentLable.x, self.contentLable.y, nameLableLayout.textBoundingSize.width, nameLableLayout.textBoundingSize.height);
            
//            self.contentLable.text = viewModel.recentMessage.contentTxt;
        }
            break;
        case MHRecentMessageTypeAttribute:
        {
            UIImage *likeImage = [UIImage imageNamed:@"icon_like_recent"];
            
            NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
            attachment.image = likeImage;
            
            
            NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
            
//            NSMutableAttributedString *myString= [[NSMutableAttributedString alloc] initWithString:@"My label text"];
//            [myString appendAttributedString:attachmentString];
//            UIFont *font = MHMomentCommentScreenNameFont;
//            likeImage = [UIImage imageWithCGImage:likeImage.CGImage scale:[UIScreen screenScale] orientation:UIImageOrientationUp];
//            NSMutableAttributedString *attachLikeText = [NSMutableAttributedString yy_attachmentStringWithContent:likeImage contentMode:UIViewContentModeCenter attachmentSize:likeImage.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
            self.contentLable.attributedText = attachmentString;
            break;
        }
            
        case MHRecentMessageTypeAsk:
        {
            NSMutableAttributedString *textAttr = [[NSMutableAttributedString alloc] initWithString:viewModel.recentMessage.contentTxt];
            textAttr.yy_font = MHMomentContentFont;
            textAttr.yy_color = MHMomentContentTextColor;
            textAttr.yy_lineBreakMode = NSLineBreakByCharWrapping;
            textAttr.yy_alignment = NSTextAlignmentLeft;
            NSMutableAttributedString   *askedAttr = [[NSMutableAttributedString alloc] initWithString:@"向我提问："];
            askedAttr.yy_color = MHMomentScreenNameTextColor;
            askedAttr.yy_font = MHMomentContentFont;
            askedAttr.yy_alignment = NSTextAlignmentLeft;
            [textAttr insertAttributedString:askedAttr atIndex:0];
            [textAttr mh_regexContentWithWithEmojiImageFontSize:16];
            self.contentLable.attributedText = textAttr.copy;
            
            YYTextContainer *nameLableContainer = [YYTextContainer containerWithSize:CGSizeMake(MH_SCREEN_WIDTH - 55*2- 30-23, MAXFLOAT)];
            nameLableContainer.maximumNumberOfRows = 0;
            nameLableLayout = [YYTextLayout layoutWithContainer:nameLableContainer text:textAttr.copy];
            self.contentyyLabel.textLayout = nameLableLayout;
            //            _nameLabel.textLayout = nameLableLayout;
//            self.tileTextLayout = nameLableLayout;
//             self.contentLable.text = viewModel.recentMessage.contentTxt;
//            [self.contentyyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.left.equalTo(self.nameLable);
//                make.top.equalTo(self.createTimeLabel.mas_bottom).offset(5);
//                make.height.mas_equalTo(nameLableLayout.textBoundingSize.height);
//            }];
            }
             break;
        case MHRecentMessageTypeAnser:
        {
//            self.contentLable.text = @"已回答";
            
            NSMutableAttributedString *textAttr = [[NSMutableAttributedString alloc] initWithString:@"已回答"];
            textAttr.yy_font = MHMomentContentFont;
            textAttr.yy_color = MHMomentContentTextColor;
            YYTextContainer *nameLableContainer = [YYTextContainer containerWithSize:CGSizeMake(MH_SCREEN_WIDTH - 55*2- 30-23, MAXFLOAT)];
            nameLableContainer.maximumNumberOfRows = 0;
            nameLableLayout = [YYTextLayout layoutWithContainer:nameLableContainer text:textAttr.copy];
            self.contentyyLabel.textLayout = nameLableLayout;
                                                   
        }
            break;
        case MHRecentMessageTypeCollection:
        {
            NSMutableAttributedString *textAttr = [[NSMutableAttributedString alloc] initWithString:@"已收藏"];
            textAttr.yy_font = MHMomentContentFont;
            textAttr.yy_color = MHMomentContentTextColor;
            YYTextContainer *nameLableContainer = [YYTextContainer containerWithSize:CGSizeMake(MH_SCREEN_WIDTH - 55*2- 30-23, MAXFLOAT)];
            nameLableContainer.maximumNumberOfRows = 0;
            nameLableLayout = [YYTextLayout layoutWithContainer:nameLableContainer text:textAttr.copy];
            self.contentyyLabel.textLayout = nameLableLayout;
//            NSMutableAttributedString *textAttr = [[NSMutableAttributedString alloc] initWithString:@"已收藏"];
//            self.contentyyLabel.text = @"已收藏";
        }
        default:
            break;
    }
//    self.
    self.contentyyLabel.textLayout = nameLableLayout;
    self.nameLableLayout = nameLableLayout;
    [self.contentyyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLable);
        make.top.equalTo(self.createTimeLabel.mas_bottom).offset(5);
        make.height.mas_equalTo(nameLableLayout.textBoundingSize.height);
    }];
//    self.contentyyLabel.height = nameLableLayout.textBoundingSize.height;
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.contentyyLabel.height = self.nameLableLayout.textBoundingSize.height;
}
-(void)dealloc
{
    NSLog(@"ccc");
}

-(YYLabel *)contentyyLabel
{
    if (!_contentyyLabel) {
        _contentyyLabel = [[YYLabel alloc] init];
        _contentyyLabel.backgroundColor = self.contentView.backgroundColor;
        /// 垂直方向中心对齐
        _contentyyLabel.textVerticalAlignment = YYTextVerticalAlignmentCenter;
        /// 异步渲染和布局
        _contentyyLabel.displaysAsynchronously = NO;
        /// 利用textLayout来设置text、font、textColor...
        _contentyyLabel.ignoreCommonProperties = YES;
        _contentyyLabel.fadeOnAsynchronouslyDisplay = NO;
        _contentyyLabel.fadeOnHighlight = NO;
        _contentyyLabel.preferredMaxLayoutWidth = MH_SCREEN_WIDTH - 55*2- 30-23;
        
    }
    return _contentyyLabel;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
